﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Usermanagment.DBConnection;
using Usermanagment.VisualDisplay;

namespace Usermanagment
{
	using Usermanagment.VisualDisplay;
    public partial class Login : Form
    {
        DBConnect dbc;

        public Login()
        {
            InitializeComponent();
			this.MaximumSize = this.MinimumSize = this.Size;
			button2.Enabled = false;
			Password.PasswordChar = '*';
			this.Text += " By Avishai.K. and Orel.E.";
			textBox1.Enabled = false;
			
        }

        private void button1_Click(object sender, EventArgs e)
        {
			if (fieldsNEmpty())
			{
				if (isipAddress())
				{
					dbc = new DBConnect();
					dbc.Sserver = Server.Text;
					dbc.Database = Database.Text;
					dbc.User = User.Text;
					dbc.Password = Password.Text;
					Boolean ans = dbc.OpenConnection();
					textBox1.Text = dbc.getMsg();
					if (textBox1.Text.Equals("Connected!"))
					{
						button1.Enabled = false;
						button2.Enabled = true;
						statSet(false);
						MainMenu tbs = new MainMenu(this.dbc);
						tbs.Visible = true;
					}
					else
					{
						User.Text = "";
						Password.Text = "";
						Database.Text = "";
					}
				}
				else
					textBox1.Text = "Please enter proper IP Address!";

			}
			else
				textBox1.Text = "Some fields are empty!";

        }

        private Boolean fieldsNEmpty()
        {
            return (Server.Text.Length > 0 && Database.Text.Length > 0 && User.Text.Length > 0 && Password.Text.Length > 0);
        }

        private Boolean isipAddress()
        {
            String s = Server.Text;
            for (int i = 0; i < s.Length; i++)
            {
                if ((s[i] < '0' || s[i] > '9') && s[i] != '.')
                    return false;
            }
           
            return true;

        }

		private void button2_Click(object sender, EventArgs e)
		{
			dbc.closeConnection();
			textBox1.Text = dbc.getMsg();
			button2.Enabled = false;
			button1.Enabled = true;
			statSet(true);
		}

		private void statSet(Boolean b)
		{
			Server.Enabled = b;
			Database.Enabled = b;
			User.Enabled = b;
			Password.Enabled = b;
		}
        
    }
}
